package com.yidiandao.business.ui.widget.listview.viewholder;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.widget.listview.ViewHolder;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 首页，免费赠送，ViewHolder
 */
public class HomeFreeHolder extends ViewHolder {

    public ImageView ivImage1;
    public TextView tvTitle1;
    public TextView tvSurplus1;
    public ImageView ivImage2;
    public TextView tvTitle2;
    public TextView tvSurplus2;
    public ImageView ivImage3;
    public TextView tvTitle3;
    public TextView tvSurplus3;
    public TextView tvMore;
    public ImageView ivFree;
    public LinearLayout root2;
    public LinearLayout root3;

    @Override
    protected void initView(View rootView) {
        tvMore = (TextView) rootView.findViewById(R.id.tv_enter);
        ivImage1 = (ImageView) rootView.findViewById(R.id.iv_image_1);
        tvTitle1 = (TextView) rootView.findViewById(R.id.tv_title_1);
        tvSurplus1 = (TextView) rootView.findViewById(R.id.tv_surplus_1);
        ivImage2 = (ImageView) rootView.findViewById(R.id.iv_image_2);
        tvTitle2 = (TextView) rootView.findViewById(R.id.tv_title_2);
        tvSurplus2 = (TextView) rootView.findViewById(R.id.tv_surplus_2);
        ivImage3 = (ImageView) rootView.findViewById(R.id.iv_image_3);
        tvTitle3 = (TextView) rootView.findViewById(R.id.tv_title_3);
        tvSurplus3 = (TextView) rootView.findViewById(R.id.tv_surplus_3);
        ivFree = (ImageView) rootView.findViewById(R.id.iv_free);
        root2 = (LinearLayout) rootView.findViewById(R.id.root_2);
        root3 = (LinearLayout) rootView.findViewById(R.id.root_3);

        ImageLoader.loadLocalImage(mContext, ivFree, R.mipmap.ic_home_free);
    }

    public void setLeaveString(TextView textView, String leave) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        SpannableString pre = TextRenderUtils.getForegroundColorText(
                UIUtils.getString(R.string.free_leave), UIUtils.getColor(R.color.goods_list_title));
        SpannableString suf = TextRenderUtils.getForegroundColorText(leave, UIUtils.getColor(R.color.btn_red));

        builder.append(pre);
        builder.append(suf);

        textView.setText(builder);
    }

    @Override
    protected int getItemLayoutId() {
        return R.layout.item_home_free;
    }

    public void refreshUI1(Goods goods1) {
        tvTitle1.setText(goods1.getGoodsName());
        ImageLoader.loadImageWithPlaceHolder(mContext, ivImage1, goods1.getGoodsThumb());
        setLeaveString(tvSurplus1, goods1.getLessNumber() + goods1.getGoodsUnit());
    }

    public void refreshUI2(Goods goods2) {
        tvTitle2.setText(goods2.getGoodsName());
        ImageLoader.loadImageWithPlaceHolder(mContext, ivImage2, goods2.getGoodsThumb());
        setLeaveString(tvSurplus2, goods2.getLessNumber() + goods2.getGoodsUnit());
    }

    public void refreshUI3(Goods goods3) {
        tvTitle3.setText(goods3.getGoodsName());
        ImageLoader.loadImageWithPlaceHolder(mContext, ivImage3, goods3.getGoodsThumb());
        setLeaveString(tvSurplus3, goods3.getLessNumber() + goods3.getGoodsUnit());
    }
}
